package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.maintain.X3MaintainPlanAdapter;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.even.OptionMainTainPlan;
import com.das.mechanic_base.bean.maintain.MainTainPlanBean;
import com.das.mechanic_base.bean.plan.PlanListBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3BottomChangePlanDialog;
import com.kproduce.roundcorners.RoundTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 保养计划
 */
public class X3BottomMaintainPlaneDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3MaintainPlanAdapter.IOnLongClickMainTainPlan, X3MaintainPlaneOptionDialog.OnItemClick, X3BottomChangePlanDialog.IOnRefreshPlan {

    private RecyclerView rlv_plan;
    private TextView tv_plan_name;
    private TextView tv_plan_count;
    private TextView tv_empty;
    //主布局
    private RelativeLayout rl_main;
    private RoundTextView rv_cancel;
    private RoundTextView rv_affirm;
    private X3MaintainPlanAdapter planAdapter;
    private long carId;
    private String carName;
    private long carMaintencenPlanId;
    private PlanListBean listBean;
    private long id;//删除计划id

    private X3BottomChangePlanDialog planDialog;
    private ArrayList<MainTainPlanBean> mainTainPlanBeanList;

    public X3BottomMaintainPlaneDialog(@NonNull Context context, long carId, String carName) {
        super(context);
        this.carId = carId;
        this.carName = carName;
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_empty = getView(R.id.tv_empty);
        rlv_plan = getView(R.id.rlv_plan);
        rv_cancel = getView(R.id.rv_cancel);
        rv_affirm = getView(R.id.rv_affirm);
        tv_plan_name = getView(R.id.tv_plan_name);
        rl_main = getView(R.id.rl_main);
        tv_plan_count = getView(R.id.tv_plan_count);

        rlv_plan.setLayoutManager(new LinearLayoutManager(mContext));
        planAdapter = new X3MaintainPlanAdapter(mContext, this);
        rlv_plan.setAdapter(planAdapter);

        rv_affirm.setOnClickListener(this);
        rv_cancel.setOnClickListener(this);

    }

    //根据原厂保养手册创建保养计划：即根据所在Brand就当前车款所设置的保养周期模板制定保养计划；
    // 若当前车辆在所在Brand不存在启用状态下的保养周期模板，则隐藏按照原厂保养计划按钮；
    private void checkPlanList(long carId){
        NetWorkHttp.getApi().listMaintenanceServcieByCarId(carId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView) mContext).bindToLife())
                .subscribe(new HttpCallBack<ArrayList<MainTainPlanBean>>() {
                    @Override
                    protected void onDone(ArrayList<MainTainPlanBean> bean) {
                        bottomBtnVisiable(bean.size());
                        mainTainPlanBeanList = bean;
                    }

                    @Override
                    protected void NoNetCallBack() {
                        super.NoNetCallBack();
                    }

                    @Override
                    protected void showError(String error) {
                    }
                });
    }

    public void showMainTainPlan(long carId) {
        NetWorkHttp.getApi().getCarMaintenancePlanList(carId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView) mContext).bindToLife())
                .subscribe(new HttpCallBack<List<PlanListBean>>() {
                    @Override
                    protected void onDone(List<PlanListBean> addStaffBeans) {
                        if (addStaffBeans != null) {
                            rl_main.setVisibility(View.VISIBLE);
                            tv_plan_name.setText(carName + mContext.getResources().getString(R.string.x3_maintain_plan) );
                            tv_plan_count.setText(String.valueOf(addStaffBeans.size()));
                            if (addStaffBeans.size() == 0){
                                tv_empty.setVisibility(View.VISIBLE);
                                rlv_plan.setVisibility(View.GONE);
                            } else {
                                tv_empty.setVisibility(View.GONE);
                                rlv_plan.setVisibility(View.VISIBLE);
                            }
                            planAdapter.setDataList(addStaffBeans);
                        }
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    private void bottomBtnVisiable(int size){
        rv_cancel.setVisibility(View.VISIBLE);
        if (size==0){
            rv_affirm.setVisibility(View.GONE);
        }else{
            rv_affirm.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_maintain_plan_dialog;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rv_affirm){
            X3RouterCenter.startOriginFactoryPlan(carId,mainTainPlanBeanList);
        }else{
            dismiss();
        }
    }

    @Override
    public void iOnClickMainTainPlan(PlanListBean obj) {
        carMaintencenPlanId = obj.carMaintencenPlanId;
        id = obj.id;
        obj.carMaintencenPlanId = obj.id;
        listBean = obj;
//        /* 删除保养计划, UI暂时未确定所以不删除 add by liugl 2021/09/18
//         */
//        X3MaintainPlaneOptionDialog noticeDialog = new X3MaintainPlaneOptionDialog(mContext, this);
//        noticeDialog.show();

        if (planDialog == null) {
            planDialog = new X3BottomChangePlanDialog(mContext);
        }
        planDialog.show();
        planDialog.showChangePlan(obj);
        planDialog.setiOnRefreshPlan(this);
    }

    @Override
    public void show() {
        super.show();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        showMainTainPlan(carId);
        checkPlanList(carId);
    }

    @Override
    public void onItemClick(int index) {
        if (0 == index) {//执行
            finishPlan();
        } else if (1 == index) {//编辑
            updatePlan();
        } else if (2 == index) {//删除
            deletePlan();
        }
    }

    private void deletePlan() {
        NetWorkHttp.getApi().deleteCarPlan(id)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView) mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object object) {
                        showMainTainPlan(carId);

                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    private void finishPlan() {
        NetWorkHttp.getApi().finishCarMaintenancePlan(carMaintencenPlanId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView) mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        dismiss();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    private void updatePlan() {
        NetWorkHttp.getApi().updateCarMaintenancePlan(listBean)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView) mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        dismiss();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void dismiss() {
        super.dismiss();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void iOnRefreshPlan() {
        showMainTainPlan(carId);
        OptionMainTainPlan plan = new OptionMainTainPlan();
        EventBus.getDefault().post(plan);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeleteMainTainPlan(OptionMainTainPlan plan) {
        showMainTainPlan(carId);
    }
}
